/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author:
 */

import {
  setStore,
  getStore,
  removeStore
} from '@/utils/store'
import {
  resolveUrlPath
} from '@/utils/util'

function eachTag(state, tag) {
  for (var key in state.tagList) {
    if (state.tagList[key].value === tag.value) {
      return key
    }
  }
  return -1
}

const tagObj = {
  label: '',
  value: '',
  query: '',
  num: '',
  close: true
}

function setFistTag(list) {
  if (list.length === 1) {
    list[0].close = true
  } else {
    list.some(a => {
      a.close = true
    })
  }
  return list
}

const state = {
  isRouterView: true,
  tagList: getStore({
    name: 'tagList'
  }) || [],
  tag: getStore({
    name: 'tag'
  }) || tagObj,
  tagWel: {
    label: '首页',
    value: '/'
  },
  tagCurrent: getStore({
    name: 'tagCurrent'
  }) || []
}
const mutations = {
  RefreshRouter: (state) => {
    state.isRouterView = !state.isRouterView
  },
  ADD_TAG: (state, action) => {
    state.tag = action
    if (action.value !== '/') {
      setStore({
        name: 'tag',
        content: state.tag,
        type: 'session'
      })
    }

    state.tagList.some((item, index) => {
      if (item.value === action.value) {
        item.query = action.query
        if (action.name && action.isUpdate !== true) item.name = action.name
        state.tagList = setFistTag(state.tagList)
        setStore({
          name: 'tagList',
          content: state.tagList,
          type: 'session'
        })
        return true
      }
    })

    if (state.tagList.some(a => a.value === action.value) || action.value === '/') return
    state.tagList.push({
      label: action.label,
      value: action.value,
      query: action.query,
      name: action.name
    })

    state.tagList = setFistTag(state.tagList)
    setStore({
      name: 'tagList',
      content: state.tagList,
      type: 'session'
    })
  },
  SET_TAG_CURRENT: (state, tagCurrent) => {
    state.tagCurrent = tagCurrent
    setStore({
      name: 'tagCurrent',
      content: state.tagCurrent,
      type: 'session'
    })
  },
  SET_TAG: (state, value) => {
    state.tagList.forEach((ele, num) => {
      if (ele.value === value) {
        state.tag = state.tagList[num]
        setStore({
          name: 'tag',
          content: state.tag,
          type: 'session'
        })
      }
    })
  },
  DEL_ALL_TAG: (state) => {
    state.tag = tagObj
    state.tagList = []
    // state.tagList.push(state.tagWel);
    removeStore({
      name: 'tag'
    })
    removeStore({
      name: 'tagList'
    })
  },
  DEL_TAG_OTHER: function(state, rightSelected) {
    if (rightSelected.value && rightSelected.label) {
      state.tagList = []
      state.tagList[0] = rightSelected
      state.tag = state.tagList[0]
      state.tagList[0].close = true
      setStore({
        name: 'tag',
        content: state.tag,
        type: 'session'
      })
      setStore({
        name: 'tagList',
        content: state.tagList,
        type: 'session'
      })
      return
    }
    state.tagList.forEach((ele, num) => {
      if (ele.value === state.tag.value) {
        state.tagList = state.tagList.slice(num, num + 1)
        state.tag = state.tagList[0]
        state.tagList[0].close = true
        setStore({
          name: 'tag',
          content: state.tag,
          type: 'session'
        })
        setStore({
          name: 'tagList',
          content: state.tagList,
          type: 'session'
        })
      }
    })
  },
  DEL_TAG: (state, action) => {
    state.tagList.forEach((ele, num) => {
      if (ele.value === action.value) {
        state.tagList.splice(num, 1)
        if (state.tagList.length < 1) {
          state.tag = tagObj
          state.tagList = []
          removeStore({
            name: 'tag'
          })
          removeStore({
            name: 'tagList'
          })
        } else {
          state.tagList = setFistTag(state.tagList)
          setStore({
            name: 'tag',
            content: state.tag,
            type: 'session'
          })
          setStore({
            name: 'tagList',
            content: state.tagList,
            type: 'session'
          })
        }
      }
    })
  }

  // MOVE_TAG: (state, source,target) => {
  //     state.tagList.forEach((ele, num) => {
  //         if (ele.value === action.value) {
  //             state.tagList.splice(num, 1)
  //             state.tagList = setFistTag(state.tagList);
  //             setStore({ name: 'tag', content: state.tag,type:'session' })
  //             setStore({ name: 'tagList', content: state.tagList,type:'session' })
  //         }
  //     })
  // }
}
const actions = {
  DEL_OWER: ({
    state,
    commit
  }) => {
    const item = state.tagCurrent[0]
    // const key = eachTag(state, item)
    // let tag
    commit('DEL_TAG', item)
    // if (item.value === state.tag.value) {
    //   tag = state.tagList[key === 0 ? key : key - 1]
    //   this.isHomeActive = false
    //   this.$router.push({
    //     path: resolveUrlPath(tag.value, tag.label),
    //     query: tag.query
    //   })
    // }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
